package com.copa.l162;

/**
 * @author copa
 * @createDate 2022-06-23 17:48
 * @function 二分查找法
 */
public class Test2 {

    public int findPeakElement(int[] nums) {
        int left = 0;
        int right = nums.length - 1;
        int mid;
        while (left < right) {
            mid = left + (right - left) / 2;
            if (nums[mid] < nums[mid + 1]) {
                // 其实就是走上坡路，如果往右走肯定找到峰值，所以把left指针调过来
                left = mid + 1;
            } else {
                // 其实就是走下坡路，如果往左走肯定找到峰值，所以把right指针调过来
                right = mid;
            }
        }
        return left;
    }

    public static void main(String[] args) {
        int[] nums = {1,2,1,3,5,6,4};
        System.out.println(new Test2().findPeakElement(nums));
    }
}
